﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace BYFW.Model.Authorization
{
    /// <summary>
    /// 权限管理系统中的M-R-O关系类：
    /// 模块M对角色R开放操作O权限，数据库表需要被 Role、Operation 和 Module 级联删除
    /// </summary>
    public class MRORelation : BOL.BaseEntity
    {
        public MRORelation()
        {
            m_tableName = "auth_mro";
            m_tblCacheManager.IsEnable = true;
        }

        protected int m_mid = 0;
        protected int m_rid = 0;
        protected int m_oid = 0;

        /// <summary>
        /// 模块的ID
        /// </summary>
        public int ModuleID
        {
            get { return m_mid; }
            set { m_mid = value; }
        }
        /// <summary>
        /// 角色的ID
        /// </summary>
        public int RoleID
        {
            get { return m_rid; }
            set { m_rid = value; }
        }
        /// <summary>
        /// 操作的ID
        /// </summary>
        public int OperationID
        {
            get { return m_oid; }
            set { m_oid = value; }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("mid", m_mid);
            m_container.Add("rid", m_rid);
            m_container.Add("oid", m_oid);
        }
        public override bool GetProperties(DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_mid = this.GetIntValue(record, "mid");
            m_rid = this.GetIntValue(record, "rid");
            m_oid = this.GetIntValue(record, "oid");
            return true;
        }

        /// <summary>
        /// 某组角色中是否允许对模块M的操作O
        /// </summary>
        /// <param name="mid">模块ID</param>
        /// <param name="oid">操作ID</param>
        /// <param name="rids">角色组</param>
        /// <returns></returns>
        public bool IsAllowed(int mid, int oid, List<int> rids)
        {
            string cond = " WHERE mid={0} AND oid={1} AND rid IN ({2})";
            string rs = "";
            for (int i = 0; i < rids.Count; i++)
            {
                rs += DB.GetVal(rids[i]) + ",";
            }
            rs = rs.TrimEnd(',').Trim();
            if (rs == "")
            {
                rs = DB.GetVal(0);
            }
            cond = string.Format(cond, mid, oid, rs);
            return this.Exist(null, cond, true);
        }
        /// <summary>
        /// 某组角色中是否允许对模块M的操作O
        /// </summary>
        /// <param name="mcode">模块Code</param>
        /// <param name="ocode">操作Code</param>
        /// <param name="rids">角色组</param>
        /// <returns></returns>
        public bool IsAllowed(string mcode, string ocode, List<int> rids)
        {
            Module m = new Module();
            m.GetProperties(mcode);
            Operation o = new Operation();
            o.GetProperties(ocode);
            return IsAllowed(m.ID, o.ID, rids);
        }
        /// <summary>
        /// 某角色是否允许对模块M的操作O
        /// </summary>
        /// <param name="mid">模块ID</param>
        /// <param name="oid">操作ID</param>
        /// <param name="rid">角色ID</param>
        /// <returns></returns>
        public bool IsAllowed(int mid, int oid, int rid)
        {
            string cond = " WHERE mid={0} AND oid={1} AND rid={2}";
            cond = string.Format(cond, mid, oid, rid);
            return this.Exist(null, cond, true);
        }
        /// <summary>
        /// 某角色是否允许对模块M的操作O
        /// </summary>
        /// <param name="mcode">模块Code</param>
        /// <param name="ocode">操作Code</param>
        /// <param name="rcode">角色Code</param>
        /// <returns></returns>
        public bool IsAllowed(string mcode, string ocode, string rcode)
        {
            Module m = new Module();
            m.GetProperties(mcode);
            Operation o = new Operation();
            o.GetProperties(ocode);
            Role r = new Role();
            r.GetProperties(rcode);
            return IsAllowed(m.ID, o.ID, r.ID);
        }

        /// <summary>
        /// 获取各种符合条件的关系
        /// </summary>
        /// <param name="mid">模块ID，小于0时，忽略此条件</param>
        /// <param name="oid">操作ID，小于0时，忽略此条件</param>
        /// <param name="rids">角色组，为null时，忽略此条件</param>
        /// <returns></returns>
        public DataTable GetRecords(int mid, int oid, List<int> rids)
        {
            string cond = "";
            if (mid >= 0)
            {
                cond += "mid=" + DB.GetVal(mid);
            }
            if (oid >= 0)
            {
                if (cond.Length > 0)
                {
                    cond += " AND ";
                }
                cond += "oid=" + DB.GetVal(oid);
            }
            if (rids != null && rids.Count > 0)
            {
                string rs = "";
                for (int i = 0; i < rids.Count; i++)
                {
                    rs += DB.GetVal(rids[i]) + ",";
                }
                if (cond.Length > 0)
                {
                    cond += " AND ";
                }
                cond += "rid IN (" + rs.TrimEnd(',') + ")";
            }
            if (cond != "")
            {
                cond = " WHERE " + cond;
            }
            return this.GetRecords(cond);
        }
        /// <summary>
        /// 获取各种符合条件的关系
        /// </summary>
        /// <param name="mcode">模块Code，为空或null时，忽略此条件</param>
        /// <param name="ocode">操作Code，为空或null时，忽略此条件</param>
        /// <param name="rids">角色组，为null时，忽略此条件</param>
        /// <returns></returns>
        public DataTable GetRecords(string mcode, string ocode, List<int> rids)
        {
            int mid = -1;
            int oid = -1;
            if (!string.IsNullOrEmpty(mcode))
            {
                Module m = new Module();
                m.GetProperties(mcode);
                mid = m.ID;
            }
            if (!string.IsNullOrEmpty(ocode))
            {
                Operation o = new Operation();
                o.GetProperties(ocode);
                oid = o.ID;
            }
            return this.GetRecords(mid, oid, rids);
        }

        /// <summary>
        /// 获取各种符合条件的关系
        /// </summary>
        /// <param name="mid">模块ID，小于0时，忽略此条件</param>
        /// <param name="oid">操作ID，小于0时，忽略此条件</param>
        /// <param name="rid">角色ID，小于0时，忽略此条件</param>
        /// <returns></returns>
        public DataTable GetRecords(int mid, int oid, int rid)
        {
            string cond = "";
            if (mid > 0)
            {
                cond += "mid=" + DB.GetVal(mid);
            }
            if (oid > 0)
            {
                if (cond.Length > 0)
                {
                    cond += " AND ";
                }
                cond += "oid=" + DB.GetVal(oid);
            }
            if (rid > 0)
            {
                if (cond.Length > 0)
                {
                    cond += " AND ";
                }
                cond += "rid=" + DB.GetVal(rid);
            }
            if (cond != "")
            {
                cond = " WHERE " + cond;
            }
            return this.GetRecords(cond);
        }
        /// <summary>
        /// 获取各种符合条件的关系
        /// </summary>
        /// <param name="mcode">模块Code，为空或null时，忽略此条件</param>
        /// <param name="ocode">操作Code，为空或null时，忽略此条件</param>
        /// <param name="rcode">角色Code，为空或null时，忽略此条件</param>
        /// <returns></returns>
        public DataTable GetRecords(string mcode, string ocode, string rcode)
        {
            int mid = 0;
            int oid = 0;
            int rid = 0;
            if (!string.IsNullOrEmpty(mcode))
            {
                Module m = new Module();
                m.GetProperties(mcode);
                mid = m.ID;
            }
            if (!string.IsNullOrEmpty(ocode))
            {
                Operation o = new Operation();
                o.GetProperties(ocode);
                oid = o.ID;
            }
            if (!string.IsNullOrEmpty(rcode))
            {
                Role r = new Role();
                r.GetProperties(rcode);
                rid = r.ID;
            }
            return this.GetRecords(mid, oid, rid);
        }
        /// <summary>
        /// 删除指定角色的权限
        /// </summary>
        /// <param name="rid">角色ID</param>
        public int DeleteByRoleID(string transID, int rid)
        {
            string cond = " WHERE rid=" + DB.GetVal(rid);
            return this.DeleteRecord(transID, cond);
        }
    }
}
